import { defineStore } from 'pinia'
import { ref } from 'vue'
import router from '@/router'
import { usePermissionStore } from '@/store'
import _XEUtils_ from 'xe-utils'

const storeSetup = () => {
  const tagList = ref([]) // 保存页签tab的数组
  const cacheList = ref([]) // keep-alive缓存的数组, 元素是组件名

  // 添加一个页签, 如果当前路由已经打开, 则不再重复添加
  const addTagItem = (item) => {
    if (tagList.value.some((i) => i.fullPath === item.fullPath)) return
    tagList.value.push(item)
  }

  // 删除一个页签
  const deleteTagItem = (fullPath, to = false, de = true) => {
    const index = tagList.value.findIndex((item) => item.fullPath === fullPath)
    if (index >= 0) {
      if (de && sessionStorage.getItem(fullPath)) {
        sessionStorage.removeItem(fullPath)
      }
      if (to) {
        router.push(to)
        tagList.value.splice(index, 1)
      } else {
        const isActive = router.currentRoute.value.fullPath === tagList.value[index]['fullPath']
        tagList.value.splice(index, 1)
        if (isActive) {
          router.push(tagList.value[tagList.value.length - 1]['fullPath'])
        }
      }
    }
  }

  // 清空页签
  const clearTagList = () => {
    const permissionStore = usePermissionStore()
    const arr = _XEUtils_.filterTree(permissionStore.routes, (item) => item.meta?.affix ?? false)
    tagList.value = [...arr]
    router.replace('/')
  }

  // 添加缓存页
  const addCacheItem = (item) => {
    if (item.name) {
      if (cacheList.value.includes(item.name)) return
      if (item.meta?.keepAlive) {
        cacheList.value.push(item.name)
      }
    }
  }

  // 更改指定路由页签名称 fullPath不传则默认当前路由
  const changeTagTitle = (title, fullPath = false) => {
    const pointFullPathPath = fullPath || router.currentRoute.value.fullPath
    const index = tagList.value.findIndex((i) => i.fullPath == pointFullPathPath)
    if (index >= 0) {
      tagList.value[index].meta.title = title
    }
  }

  // 删除一个缓存页
  const deleteCacheItem = (item) => {
    if (item.name) {
      const index = tagList.value.findIndex((i) => i.fullPath === item.fullPath)
      if (index === -1) {
        const index = cacheList.value.findIndex((n) => n === item.name)
        if (index >= 0) {
          cacheList.value.splice(index, 1)
        }
      }
    }
  }

  // 清空缓存页
  const clearCacheList = () => {
    cacheList.value = []
  }

  // 关闭当前
  const closeCurrent = (fullPath, to = false) => {
    const item = tagList.value.find((i) => i.fullPath === fullPath)
    deleteTagItem(fullPath, to)
    if (item?.name) {
      deleteCacheItem(item)
    }
  }

  // 关闭其他
  const closeOther = (fullPath) => {
    const arr = tagList.value.filter((i) => i.fullPath !== fullPath)
    arr.forEach((item) => {
      deleteTagItem(item.fullPath, false)
      if (item?.name) {
        deleteCacheItem(item)
      }
    })
  }

  // 关闭全部
  const closeAll = () => {
    clearTagList()
  }

  // 重置
  const reset = () => {
    clearTagList()
    clearCacheList()
  }

  return {
    tagList,
    cacheList,
    addTagItem,
    deleteTagItem,
    clearTagList,
    addCacheItem,
    deleteCacheItem,
    clearCacheList,
    closeCurrent,
    closeOther,
    closeAll,
    changeTagTitle,
    reset
  }
}

export const useTabsStore = defineStore('tabs', storeSetup, { persist: true })
